Skip to content

[Bug Fix] Prevent Command dialog stacking#386

Open
djalmaaraujo wants to merge 4 commits into
mainfrom
da/issue-230-command-single-dialog
Open

[Bug Fix] Prevent Command dialog stacking#386
djalmaaraujo wants to merge 4 commits into
mainfrom
da/issue-230-command-single-dialog

Conversation

@djalmaaraujo
Copy link
Copy Markdown
Contributor

Summary

  • Mark cloned Command dialog content so the controller can detect an already-open dialog
  • Refocus the existing dialog input instead of appending another modal on repeated keybindings
  • Update docs controller copy and add component markup coverage

Closes #230

Testing

  • docker run --rm -v ruby_ui_bundle:/usr/local/bundle -v /Users/djalmaaraujo/dev/linkana/ruby_ui-issue-230:/workspace -w /workspace/gem ruby:3.4.7 bash -lc 'bundle install && bundle exec rake test TEST=test/ruby_ui/command_test.rb'

@djalmaaraujo djalmaaraujo requested a review from cirdes as a code owner May 7, 2026 13:27
this.selectedIndex = -1;
}

focusDialogInput(dialog) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it re-implements Stimulus target lookup by hand and hard-codes the controller identifier inside a selector string. If the controller is ever renamed, the input target moves, or the input gets wrapped in another element, this silently stops working, and the controller is now coupled to another instance's internal markup.

Using querySelector is a code smell in hotwire

Copy link
Copy Markdown
Contributor Author

@djalmaaraujo djalmaaraujo May 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid point — querySelector + hardcoded selector + manual target lookup couples this controller to another instance's markup.

Refactored to use Stimulus outlets (af84363): split into a new ruby-ui--command-dialog controller that owns the trigger + content target and declares a ruby-ui--command outlet. Outlet connected/disconnected callbacks track the active cloned dialog, and open() calls focusInput() on the outlet instead of querying the DOM. Inner ruby-ui--command now only handles input/items/filter/keydown/dismiss, with no hasContentTarget dual-personality. Marker attribute is now declarative outlet config, not a runtime querySelector.

Replace querySelector + hardcoded selector with a static `openInstance`
reference set in connect() and cleared in disconnect(). Removes the
`data-ruby-ui--command-dialog` marker attribute and per-controller DOM
lookups, so renaming the controller or restructuring dialog markup no
longer silently breaks single-instance behavior.

Addresses review feedback on #386.
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 4 files

@djalmaaraujo djalmaaraujo requested a review from cirdes May 12, 2026 16:14
Introduce ruby-ui--command-dialog controller for the trigger/template
wrapper and keep ruby-ui--command for the cloned dialog instance. The
trigger declares a ruby-ui--command outlet matched by a marker attribute
on the cloned dialog wrapper. Outlet connect/disconnect callbacks track
the active dialog, replacing the static class field and avoiding both
querySelector and same-identifier dual-personality controller code.

- New: command_dialog_controller.js (trigger + content target + outlet)
- Strip open/openValue/content target from command_controller.js
- Rename trigger actions to ruby-ui--command-dialog#open
- Add ruby_ui__command_dialog_instance marker on cloned dialog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Multiple modals stack up when using command component in demo

2 participants